package com.sec.android.app.myfiles.activity;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.MenuItem;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.fragment.settings.AboutPageFragment;
import com.sec.android.app.myfiles.fragment.settings.SettingsFragment;
import com.sec.android.app.myfiles.info.AppConstants;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.log.SamsungAnalyticsLog;
import com.sec.android.app.myfiles.util.UiUtils;

/**
 * Created by seongsu.yoon on 2016-05-12.
 */
public class SettingsActivity extends AbsMyFilesActivity {
    public static final String MYFILES_ACTION_START_SETTINGS = "com.sec.android.app.myfiles.START_SETTINGS";
    public static final String MYFILES_ACTION_START_ABOUT_PAGE = "com.sec.android.app.myfiles.START_ABOUT_PAGE";

    public static SamsungAnalyticsLog.ScreenPath sScreenPath;
    private static Activity sActivity = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.settings_activity);
        setActionBar(R.string.myfiles_settings);

        IntentFilter filter = new IntentFilter();
        filter.addAction(MYFILES_ACTION_START_SETTINGS);
        LocalBroadcastManager.getInstance(this).registerReceiver(mStartSettingsReceiver, filter);

        UiUtils.sendLocalBroadcastIntent(getApplicationContext(), MYFILES_ACTION_START_SETTINGS, getProcessId());

        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        SettingsFragment fragment = new SettingsFragment();
        fragment.setProcessId(getProcessId());
        ft.add(R.id.settings_layout_container, fragment);
        ft.commitAllowingStateLoss();
        sActivity = this;

        Intent intent = getIntent();
        if (intent != null && MYFILES_ACTION_START_ABOUT_PAGE.equals(intent.getAction())) {
            Fragment aboutPageFragment = new AboutPageFragment();
            FragmentManager fManager = getFragmentManager();
            FragmentTransaction fTransaction = fManager.beginTransaction();
            fTransaction.replace(R.id.settings_layout_container, aboutPageFragment);
            fTransaction.addToBackStack(null);
            fTransaction.commitAllowingStateLoss();
        }
    }

    public void removeActionBarDivider() {
        ActionBar actionBar = getActionBar();
        if (actionBar != null) {
            actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar_bg));
        }
    }

    private void applyActionBarDivider(ActionBar actionBar) {
        if (actionBar != null) {
            actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.action_bar_divider));
        }
    }

    public void setActionBar(int id) {
        ActionBar actionBar = getActionBar();
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setTitle(id);
            applyActionBarDivider(actionBar);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        boolean bRet = true;

        switch (item.getItemId()) {
            case android.R.id.home:
                SamsungAnalyticsLog.sendLog(sScreenPath, SamsungAnalyticsLog.Event.NAVIGATE_UP, null);
                onBackPressed();
                break;
            default:
                bRet = super.onOptionsItemSelected(item);
        }

        return bRet;
    }

    private BroadcastReceiver mStartSettingsReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (MYFILES_ACTION_START_SETTINGS.equals(intent.getAction())) {
                if (getProcessId() != intent.getIntExtra(AppConstants.EXTRA_PROCESS_ID, getProcessId())) {
                    finish();
                }
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        sActivity = null;
        LocalBroadcastManager.getInstance(this).unregisterReceiver(mStartSettingsReceiver);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        Log.a(this, "onConfigurationChanged" + newConfig.getLayoutDirection());
        super.onConfigurationChanged(newConfig);
        UiUtils.setWindowAttribute(this, null);
        setDensityDpi(newConfig.densityDpi);
    }

    public static SamsungAnalyticsLog.ScreenPath getScreenPath() {
        return sScreenPath;
    }

    public void setScreenPath(SamsungAnalyticsLog.ScreenPath sScreenPath) {
        SettingsActivity.sScreenPath = sScreenPath;
    }

    public static Activity getsActivity() {
        return sActivity;
    }
}
